package com.blockchain.console.servlets.content.cjwt;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.common.enums.QuestionType;
import com.blockchain.console.service.content.QuestionManage;
import com.blockchain.console.service.content.entity.QuestionRecord;
import com.blockchain.console.service.content.query.QuestionQuery;
import com.blockchain.framework.http.servlet.annotation.Right;
import com.blockchain.framework.resource.PromptLevel;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.framework.service.exception.LogicalException;
import com.blockchain.framework.service.exception.ParameterException;
import com.blockchain.util.parser.EnumParser;
import com.blockchain.util.parser.IntegerParser;
import com.blockchain.util.parser.TimestampParser;

@Right(id = "C_CONTENT_CJWT_UPDATEQUESTION", name = "帮助中心-修改")
public class UpdateQuestion extends AbstractQuestionServlet{

	private static final long serialVersionUID = 1L;

	@Override
	protected void processGet(HttpServletRequest request,
			HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		QuestionManage questionManage=serviceSession.getService(QuestionManage.class);
		QuestionRecord record=questionManage.getQuestion(IntegerParser.parse(request.getParameter("id")));
		if (record == null) {
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
			return;
		}
		request.setAttribute("record", record);
		forwardView(request, response, getClass());
	}
	@Override
	protected void processPost(final HttpServletRequest request,
			final HttpServletResponse response,
			final ServiceSession serviceSession) throws Throwable {
		try {
		QuestionManage questionManage=serviceSession.getService(QuestionManage.class);
		int id = IntegerParser.parse(request.getParameter("id"));
		questionManage.updateQuestion(new QuestionQuery() {
			@Override
			public QuestionType getType() {
			    return EnumParser.parse(QuestionType.class,request.getParameter("qType"));
			}
			
			@Override
			public String getQuestion() {
				return request.getParameter("question");
			}
			
			@Override
			public String getHd() {
				return request.getParameter("hd");
			}

			@Override
			public String getQuestionEn() {
				return request.getParameter("questionEn");
			}
			
			@Override
			public String getHdEn() {
				return request.getParameter("hdEn");
			}
			
			@Override
			public String getQuestionTw() {
//				return request.getParameter("questionTw");
				return "test";
			}
			
			@Override
			public String getHdTw() {
//				return request.getParameter("hdTw");
				return "test";
			}

			@Override
			public Timestamp getTjsj() {
				return TimestampParser.parse(request.getParameter("createTime"));
			}

			@Override
			public int getPxz() {
				return Integer.parseInt(request.getParameter("questionIndex"));
			}
		}, id);
		sendRedirect(request, response, getController().getURI(request, QuestionList.class));
		} catch (ParameterException | LogicalException e) {
			prompt(request, response, PromptLevel.ERROR, e.getMessage());
			processGet(request, response, serviceSession);
		}
	}
}
